package com.jixiantubu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gemptc.bean.ReplyBean;
import com.gemptc.bean.ReplyInfoBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.entity.Reply_InfoDaoimp;

/**
 * Servlet implementation class Reply_servlet
 */
@WebServlet("/reply_servlet")
public class Reply_servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Reply_servlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		String value = request.getParameter("key");
		Date currentTime = new Date();
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(currentTime);
		Reply_InfoDaoimp reply = new Reply_InfoDaoimp();
		String topid;
		String uid ;
		String topic;
		Gson gson = new Gson();
		Type typeOfSrc = new TypeToken<List<ReplyBean>>() {
		}.getType();
		switch (value) {
		case "1":
			uid = request.getParameter("userid");
			// System.out.println(uid);
			topic = request.getParameter("topicid");
			// System.out.println(topic);
			int topicid = Integer.valueOf(topic);
			List<ReplyInfoBean> list2 = reply.selectReply( topicid);
			String resu = gson.toJson(list2, typeOfSrc);
			printWriter.write(resu);
			break;
		case "2":
			String topic1 = request.getParameter("topic");
			String rpid = request.getParameter("rpid");
			int topic_id = Integer.valueOf(topic1);
			int rp_id = Integer.valueOf(rpid);
			List<ReplyInfoBean> list3 = reply.selectReply_Reply(topic_id,rp_id);
			String result1 = gson.toJson(list3, typeOfSrc);
			
			printWriter.write(result1);
			break;
		case "3":
			
			String username = request.getParameter("be_replyid");
			String topicID = request.getParameter("topic_id");
			String tiezi_neirong = request.getParameter("content");
			String replyusername = request.getParameter("userid");
			int rank= Integer.parseInt(request.getParameter("rank"));
			int scan= Integer.parseInt(request.getParameter("reply"));
			System.out.println(username+"username"+ topicID+"topicID" +tiezi_neirong+"tiezi_neirong" +replyusername);
			boolean asd=reply.insert(Integer.parseInt(topicID), replyusername, username,
					tiezi_neirong, date, rank);
			if (asd) {
				reply.updateScan(Integer.parseInt(topicID), scan);
				System.out.println("loakalal");
			}
			break;
		case "4":
			topid = request.getParameter("topic");
			int topic_id1 = Integer.valueOf(topid);
			List<ReplyInfoBean> list4 = reply.selectReply_Reply3(topic_id1);
			String resul = gson.toJson(list4, typeOfSrc);
			System.out.println(">>>>>>>>>>++++"+resul);
			printWriter.write(resul);
			break;
		case "5":
			String username1 = request.getParameter("be_replyid");
			String topicID1 = request.getParameter("topic_id");
			String tiezi_neirong1 = request.getParameter("content");
			String replyusername1 = request.getParameter("userid");
			String rpid2 = request.getParameter("rpid");
			int rp_id2=Integer.parseInt(rpid2);
			int rank1=0;
			System.out.println(username1+"username1"+ topicID1+"topicID1" +tiezi_neirong1+"tiezi_neirong1" +replyusername1+"replyusername1");
			boolean flag=reply.insertReply(rp_id2, Integer.parseInt(topicID1), Integer.parseInt(username1),  Integer.parseInt(replyusername1), tiezi_neirong1, date,rank1);
			if (flag) {
				List<ReplyInfoBean> list5 = reply.selectReply_Reply3(Integer.parseInt(topicID1));
				String resul2 = gson.toJson(list5, typeOfSrc);
				System.out.println(">>>>>>>>>>++++"+resul2);
				printWriter.write(resul2);
			}
			break;
		case "6":
			topid=request.getParameter("topic");
			List<ReplyInfoBean> list5=reply.selectLouzhu(Integer.parseInt(topid));
			String resul5 = gson.toJson(list5, typeOfSrc);
			printWriter.write(resul5);
			break;
		case "7":
			topid=request.getParameter("topic");
			uid = request.getParameter("userid");
			System.out.println("------"+Integer.parseInt(uid)+Integer.parseInt(topid));
			boolean isBe=reply.selectCollect(Integer.parseInt(uid),Integer.parseInt(topid));
			if (!isBe) {
				System.out.println(isBe);
				boolean falg=reply.insertCollect(Integer.parseInt(uid),Integer.parseInt(topid));
				System.out.println(falg);
				if (falg) {
					printWriter.write("成功");
				}else{
					printWriter.write("失败");
				}
			}else {
				printWriter.write("已存在");
			}
			break;
		case "8":
			uid = request.getParameter("uid");
			// System.out.println(uid);
			topic = request.getParameter("topic");
			// System.out.println(topic);
			int userid1 = Integer.valueOf(uid);
			int topicid1 = Integer.valueOf(topic);
			List<ReplyInfoBean> list21 = reply.selectByTime(userid1, topicid1);
			String resu1 = gson.toJson(list21, typeOfSrc);
			printWriter.write(resu1);
			break;
		default:
			break;
		}
	}
}
